package com.itheima.sh.controller;

//Mybatis-Plus 为了开发更加快捷，对业务层也进行了封装,所创建的类来测UserServiceImpl的test方法


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.sh.pojo.User;
import com.itheima.sh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController  //对象转json 传给前端
@RequestMapping("user")  //请求前缀
public class UserController {

    @Autowired
    UserService userService;

    //1.新增
    @PostMapping
    public User add(@RequestBody User user) {
        //@RequestBody将json转为对象
        userService.save(user);
        return user;
    }

    //2.删除
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable("id") Long id) {
        userService.removeById(id);
        return true;
    }

    //3.更新
    @PutMapping("/{id}")
    public boolean update(@PathVariable("id") Long id, @RequestBody User user) {
        userService.update(user, Wrappers.<User>lambdaQuery().eq(User::getId, id));
        return true;
    }

    //4.1查询
    @GetMapping("/{id}")
    public User findOne(@PathVariable("id") Long id) {
        return userService.getById(id);
    }

    //4.2查询 返回集合
    @PostMapping("findList")
    public List<User> findList(@RequestBody User user) {
        LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
        List<User> list = userService.list();
        return list;
    }

    //4.3查询 分页查询
    @PostMapping("findList/{page}/{size}")
    public Page<User> findPage(@PathVariable("page") Long current, @PathVariable("size") Long size, @RequestBody User user) {
        LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
        Page<User> page = new Page<>(current, size);
        return userService.page(page);
    }
}
